package edu.scau.cxq.gp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.cxq.gp.common.utils.MD5Utils;
import edu.scau.cxq.gp.service.IUserService;
import edu.scau.cxq.gp.entity.dto.UserDTO;
import edu.scau.cxq.gp.entity.User;
import edu.scau.cxq.gp.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.scau.cxq.gp.entity.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 存储用户信息 服务实现类
 * </p>
 *
 * @author CXQ
 * @since 2022-01-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    /**
     *  通过校验密码判断身份 如果成功返回一个User
     * @param username
     * @param password
     * @return
     */
    @Override
    public UserVO getUser(String username, String password) {
        //条件构造器
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account", username);

        //查询当前用户
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            return null;
        }

        //对用户输入密码加密
        password = MD5Utils.MD5Upper(password, user.getSalt());

        // 密码错误则返回空值
        if (!password.equals(user.getUserPasswd())) {
            return null;
        }

        // 密码正确返回vo对象
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return userVO;
    }

    @Override
    public PageInfo<UserVO> getUserVOList(Integer currentPage, Integer size, UserVO userVO) {
        PageHelper.startPage(currentPage, size);
        List<UserVO> userVOS = userMapper.selectUserVOList(userVO);
        PageInfo<UserVO> userVOPageInfo = new PageInfo<>(userVOS);
        return userVOPageInfo;
    }

    /**
     * 添加用户
     * @param userDTO
     * @return
     */
    @Override
    public boolean registerUser(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        user.setUserType(1);

        // 生成随机盐
        String salt = MD5Utils.getSalt();
        System.out.println("salt:["+ salt +"]");
        String password = MD5Utils.MD5Upper(userDTO.getUserPasswd(), salt);
        user.setSalt(salt);
        user.setUserPasswd(password);

        return userMapper.insert(user) == 1;
    }

    @Override
    public User checkUser(User user) {

        //条件构造器
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_account", user.getUserAccount());

        User userInfo = userMapper.selectOne(userQueryWrapper);
        if (userInfo == null) {
            return null;
        }
        String password = MD5Utils.MD5Upper(user.getUserPasswd(), userInfo.getSalt());

        assert password != null;
        if (!password.equals(userInfo.getUserPasswd())) {
            return null;
        }

        return userInfo;
    }

}
